//index.js
//获取应用实例
const app = getApp()

Page({
    data: {
        motto: 'Hello World',
        userInfo: {},
        hasUserInfo: false,
        canIUse: wx.canIUse('button.open-type.getUserInfo')
    },
    //事件处理函数
    bindViewTap: function () {
        wx.navigateTo({
            url: '../logs/logs'
        })
    },
    onLoad: function () {
        if (app.globalData.userInfo) {
            this.setData({
                userInfo: app.globalData.userInfo,
                hasUserInfo: true
            })
        } else if (this.data.canIUse) {
            // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
            // 所以此处加入 callback 以防止这种情况
            app.userInfoReadyCallback = res => {
                this.setData({
                    userInfo: res.userInfo,
                    hasUserInfo: true
                })
            }
        } else {
            // 在没有 open-type=getUserInfo 版本的兼容处理
            wx.getUserInfo({
                success: res => {
                    app.globalData.userInfo = res.userInfo
                    this.setData({
                        userInfo: res.userInfo,
                        hasUserInfo: true
                    })
                }
            })
        }
    },
    getUserInfo: function (e) {
        console.log(e)
        app.globalData.userInfo = e.detail.userInfo
        this.setData({
            userInfo: e.detail.userInfo,
            hasUserInfo: true
        })
    },
    initUserInfo: function () {
        const openid = wx.getStorageSync('openid');
        const that = this;
        if (openid) {
            wx.getUserInfo({
                success: function (res) {
                    that.setLoginByUserInfo(openid, res.userInfo);
                },
                fail: function () {
                    console.log("获取失败1！");
                    that.initUserInfoFail();
                },
                complete: function () {
                    // complete
                    console.log("获取用户信息完成！")
                }
            })
        } else {
            wx.login({
                success: function (res) {
                    console.log(res.code)
                    if (res.code) {
                        wx.getUserInfo({
                            withCredentials: true,
                            success: function (res_user) {
                                that.setLoginByWeixin(res.code, res_user);
                            }, fail: function () {
                                console.log("获取失败2！");
                                that.initUserInfoFail();
                            }, complete: function (res) {

                            }
                        })
                    }
                }
            })

        }
    },
    initUserInfoFail: function () {
        const that = this;
        wx.showModal({
            title: '警告通知',
            content: '您点击了拒绝授权,将无法正常显示个人信息,点击确定重新获取授权。',
            success: function (res) {
                if (res.confirm) {
                    wx.openSetting({
                        success: (res) => {
                            if (res.authSetting["scope.userInfo"]) {////如果用户重新同意了授权登录
                                wx.login({
                                    success: function (res_login) {
                                        if (res_login.code) {
                                            wx.getUserInfo({
                                                withCredentials: true,
                                                success: function (res_user) {
                                                    that.setLoginByWeixin(res_login.code, res_user);
                                                }
                                            })
                                        }
                                    }
                                });
                            }
                        }, fail: function (res) {

                        }
                    })

                }
            }
        })
    },
    setLoginByWeixin: function (code, res_user) {
        const that = this;
        wx.request({
            url: app.apiURL + 'auth/loginByWeixin',
            data: {
                code: code,
                encryptedData: res_user.encryptedData,
                iv: res_user.iv
            },
            method: 'POST',
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                that.globalData.userInfo = JSON.parse(res.data);
                that.globalData.openid = res.data.openId;
                wx.setStorageSync('openid', res.data.openId);
                wx.setStorageSync('userInfo', res.data);
            }
        })
    },
    setLoginByUserInfo: function (openid, userInfo) {
        const that = this;
        wx.request({
            url: app.apiURL + 'auth/getUserInfo',
            data: {
                openid: openid,
                userInfo: userInfo
            },
            method: 'POST',
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                that.globalData.userInfo = JSON.parse(res.data);
                that.globalData.openid = res.data.openId;
                //wx.setStorageSync('openid', res.data.openId);
                wx.setStorageSync('userInfo', res.data);
            }
        })
    },
})
